let getRequestString = function(url, name) {
	var theRequest = new Object();
	var names;
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		var strs = str.split("&");
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	names = theRequest[name]
	if (names != undefined) {
		console.log(names)
		return names
	} else {
		return null
	}
}
let getQueryString = function(url, name) {
	console.log("url = " + url)
	console.log("name = " + name)
	var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
	var r = url.substr(1).match(reg)
	if (r != null) {
		console.log("r = " + r)
		console.log("r[2] = " + r[2])
		return r[2]
	}
	return null;
}
// 反转字节  小程序返回内容大端在前小端在后
let reverseText = function(uuid) {
	let str = uuid.split('-').join('')
	let arr = str.split('')
	let plaintText = ''
	for (var i = 0; i < arr.length; i = i + 2) {
		if (i % 2 == 0) {
			let num = arr[i]
			arr[i] = arr[i + 1]
			arr[i + 1] = num
		}
	}
	let newArr = arr.reverse()
	plaintText = newArr.join('')
	return plaintText
}

let hex2int = function(hex) {
	var len = hex.length,
		a = new Array(len),
		code;
	for (var i = 0; i < len; i++) {
		code = hex.charCodeAt(i);
		if (48 <= code && code < 58) {
			code -= 48;
		} else {
			code = (code & 0xdf) - 65 + 10;
		}
		a[i] = code;
	}

	return a.reduce(function(acc, c) {
		acc = 16 * acc + c;
		return acc;
	}, 0);
}
//CRC8 MAXIM x8+x5+x4+1，CRC8多项式
// 输入数据为字符串`0xFFFF`
let encodeCrc8 = function(hex) {
	var _crc8 = [
		0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83,
		0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
		0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e,
		0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc,
		0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0,
		0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62,
		0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d,
		0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
		0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5,
		0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07,
		0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58,
		0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a,
		0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6,
		0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
		0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b,
		0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9,
		0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f,
		0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
		0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92,
		0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50,
		0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c,
		0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee,
		0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1,
		0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
		0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49,
		0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b,
		0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4,
		0x95, 0xcb, 0x29, 0x77, 0xf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0x65, 0x54, 0x39, 0x08, 0x5b, 0x6a, 0xfd, 0xcc,
		0x9f, 0xae, 0x80, 0xb1, 0xe2, 0xd3, 0x44, 0x75, 0x26, 0x17, 0xfc, 0xcd,
		0x9e, 0xaf, 0x38, 0x09, 0x5a, 0x6b, 0x45, 0x74, 0x27, 0x16, 0x81, 0xb0, 0xe3,
		0xd2, 0xbf, 0x8e, 0xdd, 0xec, 0x7b, 0x4a, 0x19, 0x28, 0x06, 0x37, 0x64,
		0x55, 0xc2, 0xf3, 0xa0, 0x91, 0x47, 0x76, 0x25, 0x14, 0x83, 0xb2, 0xe1, 0xd0,
		0xfe, 0xcf, 0x9c, 0xad, 0x3a, 0x0b, 0x58, 0x69, 0x04, 0x35, 0x66, 0x57,
		0xc0, 0xf1, 0xa2, 0x93, 0xbd, 0x8c, 0xdf, 0xee, 0x79, 0x48, 0x1b, 0x2a, 0xc1,
		0xf0, 0xa3, 0x92, 0x05, 0x34, 0x67, 0x56, 0x78, 0x49, 0x1a, 0x2b, 0xbc,
		0x8d, 0xde, 0xef, 0x82, 0xb3, 0xe0, 0xd1, 0x46, 0x77, 0x24, 0x15, 0x3b, 0x0a,
		0x59, 0x68, 0xff, 0xce, 0x9d, 0xac
	]
	var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function(h) {
		return parseInt(h, 16)
	}))
	var ucLen = typedArray.length;
	var ucPtr = typedArray;
	var ucCRC8 = 0x00;
	var i = 0;
	while (ucLen--) {
		ucCRC8 = _crc8max[ucCRC8 ^ ucPtr[i]];
		i++;
	}
	return ucCRC8.toString(16).toUpperCase();
}

// 去除空格（全部）
export function trimAll(ele) {
	if (typeof ele === 'string') {
		return ele.split(/[\t\r\f\n\s]*/g).join('');

	} else {
		console.error(`${typeof ele} is not the expected type, but the string type is expected`)
	}

}
// 十六进制 20 a1 07 00  翻转  转 10 进制
export function hexToDec(hex) {
	// 定义一个变量，用于存储转换后的十进制数
	let aaa = trimAll(hex)
	let dec = 0;
	// An highlighted block
	var bbb = [...aaa];
	var ccc = aaa.split('');
	var a, b;
	var aIndex = 0;
	var bIndex = 1;
	var arr = [];
	bbb.forEach((str, index) => {
		if (index % 2 === 0) {
			a = str;
			aIndex += 1
		} else {
			b = str
			bIndex += 1
		}
		if (a && b && (bIndex - aIndex === 1)) {
			arr.push(a + b)
		}
	});
	let newStr = arr.reverse().join('')
	dec = parseInt(newStr, 16) / 100
	// 返回转换后的十进制数
	return dec;
}

function PrefixInteger(num, n) {
	return (Array(n).join(0) + num).slice(-n);
}
//  10 进制   转十六进制5000-> 20 a1 07 00
export function decToHex(num, n) {
	// 定义一个变量，用于存储转换后的十六进制数
	let dec = '';
	let aaa = (num * 100).toString(16)
	aaa = PrefixInteger(aaa, n)
	// console.log(aaa);
	// An highlighted block
	var bbb = [...aaa];
	var ccc = aaa.split('');
	var a, b;
	var aIndex = 0;
	var bIndex = 1;
	var arr = [];
	bbb.forEach((str, index) => {
		if (index % 2 === 0) {
			a = str;
			aIndex += 1
		} else {
			b = str
			bIndex += 1
		}
		if (a && b && (bIndex - aIndex === 1)) {
			arr.push(a + b)
		}
	});
	let newStr = arr.reverse().join(' ').toUpperCase()
	dec = newStr
	// 返回转换后的十进制数
	return dec;
}
/*
 * 版本号比较方法
 * 传入两个字符串，当前版本号：curV；比较版本号：reqV
 * 调用方法举例：compare("1.1","1.2")，将返回false
 */
let compare=function(curV, reqV) {
	if (curV && reqV) {
		//将两个版本号拆成数字
		var arr1 = curV.split('.'),
			arr2 = reqV.split('.');
		var minLength = Math.min(arr1.length, arr2.length),
			position = 0,
			diff = 0;
		//依次比较版本号每一位大小，当对比得出结果后跳出循环（后文有简单介绍）
		while (position < minLength && ((diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0)) {
			position++;
		}
		diff = (diff != 0) ? diff : (arr1.length - arr2.length);
		//若curV大于reqV，则返回true
		return diff > 0;
	} else {
		//输入为空
		console.log("版本号不能为空");
		return false;
	}
}

// 浮点数精确计算减法
function sub(num1, num2) {  
	if (parseFloat(num1).toString() == "NaN" || parseFloat(num2).toString() == "NaN") return;
	var r1 = 0, r2 = 0;
	try {
  		r1 = num1.toString().split(".")[1].length;
	} catch (e) {
	}
	try {
  		r2 = num2.toString().split(".")[1].length;
	} catch (e) {
	}
	var m = Math.pow(10, Math.max(r1, r2));
  		return (num1 * m - num2 * m) / m;
}
// 浮点数精确计算乘法

function mul(num1, num2) { 
	if (parseFloat(num1).toString() == "NaN" || parseFloat(num2).toString() == "NaN") return; 
	var m = 0, s1 = num1.toString(), s2 = num2.toString();
	try {
  		m += s1.split(".")[1].length
	} catch (e) {
	}
	try {
	   m += s2.split(".")[1].length
	} catch (e) {
	}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}


module.exports = {
	getRequestString: getRequestString,
	getQueryString: getQueryString,
	reverseText: reverseText,
	hex2int: hex2int,
	encodeCrc8: encodeCrc8,
	hexToDec: hexToDec,
	compare:compare,
	sub:sub,
	mul:mul,
}